import { Body, Controller, Get, HttpException, Post } from "@nestjs/common";
import { info } from "console";
import { getRoomInfo, wsDto } from "src/dto/room.dto";
import { error, success } from "src/utils/common";
import { RoomService } from "./room.service";

@Controller("room")
export class RoomController {
  constructor(private roomService: RoomService) {}

  @Get("getRoomList")
  async getRoomList() {
    const res = await this.roomService.getRoomList();
    if (res) {
      return success("ok", res);
    }
    return error("");
  }

  @Post("createRoom")
  async createRoom() {
    const type = await this.roomService.createRoom();
    if (type) {
      success();
    } else {
      error("创建失败");
    }
  }

  @Post("getWebSocketUrl")
  async getWebSocketUrl(@Body() wsBody: wsDto) {
    const res = await this.roomService.getWebSocket({
      room_id: wsBody.room_id,
      room_password: wsBody.room_password,
    });
    if (res) {
      return success("成功", { tick: res });
    } else {
      return error("获取链接失败");
    }
  }

  @Post("getRoomInfo")
  async getRoomInfo(@Body() info: getRoomInfo) {
    const data = await this.roomService.getRoomInfo(info.room_id);
    if (data) {
      data.room_password = null;
      return success("查询成功", data);
    } else {
      throw new HttpException({ code: 301, msg: "房间id不存在" }, 200);
    }
  }
}
